package com.zipking.cloud.springbootmybatis.persistance.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.zipking.cloud.springbootmybatis.persistance.entity.TMenu;
import com.zipking.cloud.springbootmybatis.persistance.mapper.TMenuMapper;
import com.zipking.cloud.springbootmybatis.persistance.service.ITMenuService;
import com.zipking.cloud.springbootmybatis.util.PageUtils;
import com.zipking.cloud.springbootmybatis.util.PagerUtilsExt;
import com.zipking.cloud.springbootmybatis.util.PaginationHelper;
import com.zipking.cloud.springbootmybatis.util.TreeUtil;
import com.zipking.cloud.springbootmybatis.vo.MenuVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kingbird
 * @since 2024-08-29
 */
@Service
public class TMenuServiceImpl extends ServiceImpl<TMenuMapper, TMenu> implements ITMenuService {

    @Override
    public PaginationHelper<MenuVo> getMenuByPage(Integer pageSize, Integer pageNum) {


        // 1、查询所有分类
        List<MenuVo> tMenuList = this.baseMapper.getMenuByPage();


        // 2、组装成父子树形结构 每条均为parentId=0 的根节点,其children包含其子节点
        List<MenuVo> menuList = TreeUtil.makeTree(tMenuList, x -> x.getPId()==-1, (x, y) -> x.getId().equals(y.getPId()), MenuVo::setSubMenus);


        // 3、分页
       /* List<MenuVo> page = new ArrayList<>();
        int start = (pageNum - 1) * pageSize;
        for (int i = start; i < (start + pageSize > testTrees.size() ? testTrees.size() : start + pageSize); i++) {
            page.add(testTrees.get(i));
        }

        PageUtils pageUtils = new PageUtils(page, tMenuList.size(), pageSize, pageNum);*/
        PaginationHelper<MenuVo> pageInfo = PaginationHelper.create(menuList, pageNum, pageSize, list -> list.size());
        /* PageInfo<MenuVo> pageInfo = pagerUtilsExt.getPageInfo(pageNum, pageSize, menuList);*/
        return pageInfo;

    }

    @Override
    public PageInfo<TMenu> getMenuByPage2(Integer pageSize, Integer currentPage) {
        // 一级菜单
        LambdaQueryWrapper<TMenu> wrapper = new LambdaQueryWrapper();
        wrapper.eq(TMenu::getPid, -1);
        List<TMenu> menuList = this.list(wrapper);
        // 子菜单
        setMenuChildren(menuList);
        PagerUtilsExt pagerUtilsExt=new PagerUtilsExt();
        PageInfo<TMenu> pageInfo = pagerUtilsExt.getPageInfo(currentPage, pageSize, menuList);
        return pageInfo;

    }

    private void setMenuChildren(List<TMenu> menuList) {
        if(menuList != null) {
            for (TMenu menu:menuList) {
                LambdaQueryWrapper<TMenu> subWrapper = new LambdaQueryWrapper();
                subWrapper.eq(TMenu::getPid, menu.getId());
                List<TMenu> subMenuList = this.list(subWrapper);
                menu.setChildren(subMenuList);
                // 递归
                setMenuChildren(subMenuList);
            }
        }
    }

}
